package glassline.test;

import java.util.Timer;
import java.util.TimerTask;

import transducer.TChannel;
import transducer.TEvent;
import transducer.Transducer;
import glassline.ConveyorAgent;
import glassline.Glass;
import glassline.OvenAgent;
import glassline.ShipperAgent;
import glassline.interfaces.Conveyor;
import glassline.interfaces.Oven;
import glassline.interfaces.Shipper;

public class IntegrationTest2 {
	static ConveyorAgent ca1, ca2;
	static ShipperAgent sa;
	static OvenAgent oa;
	static Transducer t = new Transducer();
	static Object[] args1 = {0};
	
	static Timer timer = new Timer();
	
	public static void main(String[] argc){
		ca1 = new ConveyorAgent("Conveyor 1", t);
		ca2 = new ConveyorAgent("Conveyor 2", t, 1);
		sa = new ShipperAgent("Shipper", t);
		oa = new OvenAgent("Oven", t);
		
		sa.setConveyor(ca2);
		
		oa.setPostConveyor(ca2);
		oa.setPrevConveyor(ca1);

		ca1.msgHereIsGlass(new Glass());
		ca1.msgHereIsGlass(new Glass());

		ca1.setOven(oa);
		
		ca2.setOvenWithoutmessage(oa);
		ca2.setShipper(sa);
		
		ca1.startThread();
		ca2.startThread();
		sa.startThread();
		oa.startThread();
		
		
		timer.schedule(new TimerTask(){
			public void run(){
				fireEvent1();
			}
		}, 1000);
		
	}
	
	public static void fireEvent1(){
		//Simulates glass hitting the first sensor of the oven
		System.out.println("*****Firing 11 on Channel SENSORS for OVEN");
		args1[0] = 11;
		t.fireEvent(TChannel.SENSORS, TEvent.OVEN, args1);
		timer.schedule(new TimerTask(){
			public void run(){
				fireEvent2();
			}
		}, 4000);
		
		
	}
	
	public static void fireEvent2(){
		//Simulates glass hitting the second sensor of the oven
		System.out.println("*****Firing 11 on Channel SENSORS for C2");
		args1[0] = 11;
		t.fireEvent(TChannel.SENSORS, TEvent.C2, args1);
		timer.schedule(new TimerTask(){
			public void run(){
				fireEvent3();
			}
		}, 1000);
		
	}
	
	public static void fireEvent3(){
		System.out.println("*****Firing 11 on Channel SENSORS for SHIPPER");
		args1[0] = 11;
		t.fireEvent(TChannel.SENSORS, TEvent.SHIPPER, args1);
		args1[0] = 10;
		System.out.println("*****Firing 10 on Channel SENSORS for C2");
		t.fireEvent(TChannel.SENSORS,TEvent.C2, args1);
		timer.schedule(new TimerTask(){
			public void run(){
				fireEvent4();
			}
		}, 1000);
	}
	
	public static void fireEvent4(){
		System.out.println("*****Firing 21 on Channel SENSORS for SHIPPER");
		args1[0] = 21;
		t.fireEvent(TChannel.SENSORS, TEvent.SHIPPER, args1);
		System.out.println("*****Firing 10 on Channel SENSORS for SHIPPER");
		args1[0] = 10;
		t.fireEvent(TChannel.SENSORS, TEvent.SHIPPER, args1);
		timer.schedule(new TimerTask(){
			public void run(){
				fireEvent5();
			}
		}, 1000);
	}
	
	public static void fireEvent5(){
		System.out.println("*****Firing 11 on Channel SENSORS for SHIPPER");
		args1[0] = 11;
		t.fireEvent(TChannel.SENSORS, TEvent.SHIPPER, args1);
	}

}
